package com.cip.common.util;

import java.util.List;


import org.acegisecurity.context.SecurityContext;
import org.acegisecurity.context.SecurityContextHolder;
import org.acegisecurity.userdetails.UserDetails;

import com.cip.master.model.Album;
import com.cip.admin.model.User;
import com.cip.admin.service.FriendsService;
import com.cip.admin.service.UserManager;
import com.cip.master.service.AlbumService;

public class SecurityUtil {

	private static UserManager userManager;

	public static ThreadLocal<User> threadLocalUser = new ThreadLocal<User>();
	
	private static FriendsService friendsService;
	
	private static AlbumService albumService;
	
	//private List<User> userList;
	
	//List<User> listofSuggests;



	public final UserManager getUserManager() {
		return userManager;
	}

	public final void setUserManager(UserManager userManager) {
		SecurityUtil.userManager = userManager;
	}

	public static User getLoggedInUser() throws Exception {
		SecurityContext securityContext = (SecurityContext) SecurityContextHolder
				.getContext();
		UserDetails userDetails = (UserDetails) securityContext
				.getAuthentication().getPrincipal();
		User user = userManager.getUserByUsername(userDetails.getUsername());
		getThreadLocalUser().set(user);
		return user;
	}

	public static FriendsService getFriendsService() {
		return friendsService;
	}

	public static void setFriendsService(FriendsService friendsService) {
		SecurityUtil.friendsService = friendsService;
	}
	
	public static void setUserThreadLocal(User user) {
		getThreadLocalUser().set(user);
	}

	public static ThreadLocal<User> getThreadLocalUser() {
		return threadLocalUser;
	}

	public static void setThreadLocalUser(ThreadLocal<User> threadLocalUser) {
		SecurityUtil.threadLocalUser = threadLocalUser;
	}

	public static List<User> getUserList() throws Exception {
		return friendsService.getAllFriendsById(getLoggedInUser().getId());
	}

	public static List<User> getListofSuggests() throws Exception {
		return friendsService.getSuggestedFriendsList(getLoggedInUser());		
	}

	public static Album getAlbum() throws Exception {
		Album album= new Album();
		List<Album> albums=albumService.getAllAlbum();
		if(albums.size() > 0){
			album=albums.get(0);
		}
		
		return album;
	}
	
	public static AlbumService getAlbumService() {
		return albumService;
	}

	public static void setAlbumService(AlbumService albumService) {
		SecurityUtil.albumService = albumService;
	}
	
}
